﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.TransportMgtDetailServiceRef;

    public class DeliveryOrderDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected HtmlForm F8013;
        protected HtmlInputHidden hdnDeliveryOrderID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnPageQty;
        protected HtmlInputHidden hdnPageType;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlInputHidden hdnType;
        protected Label lblAcceptQty;
        protected Label lblAllocateRuleDetail;
        protected Label lblDeliveryOrderID;
        protected Label lblLineID;
        protected Label lblLoadingQty;
        protected Label lblLostQty;
        protected Label lblMsg;
        protected Label lblOriginalQty;
        protected Label lblOwnerID;
        protected Label lblPackID;
        protected Label lblRejectedQty;
        protected Label lblRemark;
        protected Label lblSkuDescr;
        protected Label lblSkuID;
        protected Label lblUdf1;
        protected Label lblUdf10;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf4;
        protected Label lblUdf5;
        protected Label lblUdf6;
        protected Label lblUdf7;
        protected Label lblUdf8;
        protected Label lblUdf9;
        protected Label lblUomID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAcceptQty;
        protected TextBox txtDeliveryOrderID;
        protected TextBox txtLineID;
        protected TextBox txtLoadingQty;
        protected TextBox txtLostQty;
        protected TextBox txtOriginalQty;
        protected TextBox txtOwnerID;
        protected TextBox txtPackID;
        protected TextBox txtRejectedQty;
        protected TextBox txtRemark;
        protected TextBox txtSkuDescr;
        protected TextBox txtSkuID;
        protected TextBox txtUdf1;
        protected TextBox txtUdf10;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;
        protected TextBox txtUdf6;
        protected TextBox txtUdf7;
        protected TextBox txtUdf8;
        protected TextBox txtUdf9;
        protected TextBox txtUomID;
        protected UpdatePanel up1;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("DeliveryOrder.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "6", base.UrlEncode(this.hdnDeliveryOrderID.Value.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDeliveryOrderID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F8100_011");
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F8100_012");
            }
            else if (string.IsNullOrEmpty(this.txtOriginalQty.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F8100_013");
            }
            else
            {
                bool success = false;
                try
                {
                    success = this.InsertOrUpdateDeliveryOrderDetail();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F8100_014") + ex.Message;
                    return;
                }
                if (success)
                {
                    base.Response.Redirect(string.Format("DeliveryOrder.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "6", base.UrlEncode(this.hdnDeliveryOrderID.Value.Trim())), false);
                }
            }
        }

        private bool InsertOrUpdateDeliveryOrderDetail()
        {
            decimal tempDec = 0.0M;
            bool success = false;
            DeliveryOrderDetailInfo deliveryOrderDetailInfo = null;
            if (base.DataEntityViewState != null)
            {
                deliveryOrderDetailInfo = base.DataEntityViewState as DeliveryOrderDetailInfo;
            }
            else
            {
                deliveryOrderDetailInfo = new DeliveryOrderDetailInfo();
            }
            deliveryOrderDetailInfo.Status = "New";
            deliveryOrderDetailInfo.IsActive = "Y";
            deliveryOrderDetailInfo.DeliveryOrderID = this.txtDeliveryOrderID.Text.Trim();
            deliveryOrderDetailInfo.LineID = int.Parse(this.txtLineID.Text.Trim());
            deliveryOrderDetailInfo.OwnerID = this.txtOwnerID.Text.Trim();
            deliveryOrderDetailInfo.SkuID = this.txtSkuID.Text.Trim();
            deliveryOrderDetailInfo.UomID = this.txtUomID.Text.Trim();
            if (decimal.TryParse(this.txtOriginalQty.Text.Trim(), out tempDec))
            {
                deliveryOrderDetailInfo.OriginalQty = decimal.Parse(this.txtOriginalQty.Text.Trim());
            }
            else
            {
                deliveryOrderDetailInfo.OriginalQty = tempDec;
            }
            if (decimal.TryParse(this.txtLoadingQty.Text.Trim(), out tempDec))
            {
                deliveryOrderDetailInfo.LoadingQty = decimal.Parse(this.txtLoadingQty.Text.Trim());
            }
            else
            {
                deliveryOrderDetailInfo.LoadingQty = tempDec;
            }
            deliveryOrderDetailInfo.Udf1 = this.txtUdf1.Text.Trim();
            deliveryOrderDetailInfo.Udf2 = this.txtUdf2.Text.Trim();
            deliveryOrderDetailInfo.Udf3 = this.txtUdf3.Text.Trim();
            deliveryOrderDetailInfo.Udf4 = this.txtUdf4.Text.Trim();
            deliveryOrderDetailInfo.Udf5 = this.txtUdf5.Text.Trim();
            deliveryOrderDetailInfo.Udf6 = this.txtUdf6.Text.Trim();
            deliveryOrderDetailInfo.Udf7 = this.txtUdf7.Text.Trim();
            deliveryOrderDetailInfo.Udf8 = this.txtUdf8.Text.Trim();
            deliveryOrderDetailInfo.Udf9 = this.txtUdf9.Text.Trim();
            deliveryOrderDetailInfo.Udf10 = this.txtUdf10.Text.Trim();
            deliveryOrderDetailInfo.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(this.hdnType.Value) && (this.hdnType.Value == "edit"))
            {
                deliveryOrderDetailInfo.UpdatedBy = base.UserInfo.UserID;
                if (!string.IsNullOrEmpty(TransportDetailMgtServiceClientProxy.GetInstance().UpdateDeliveryOrderDetailInfo(deliveryOrderDetailInfo)))
                {
                    success = true;
                }
                return success;
            }
            deliveryOrderDetailInfo.CreatedBy = base.UserInfo.UserID;
            deliveryOrderDetailInfo.UpdatedBy = base.UserInfo.UserID;
            if (!string.IsNullOrEmpty(TransportDetailMgtServiceClientProxy.GetInstance().InsertDeliveryOrderDetailInfo(deliveryOrderDetailInfo)))
            {
                success = true;
            }
            return success;
        }

        private void LoadDate()
        {
            this.txtDeliveryOrderID.Enabled = false;
            this.txtLineID.Enabled = false;
            this.txtOwnerID.Enabled = false;
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                string from = base.Request.QueryString["from"];
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                string deliveryOrderID = base.Request.QueryString["ID"];
                this.txtDeliveryOrderID.Text = deliveryOrderID;
                this.hdnDeliveryOrderID.Value = deliveryOrderID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"]))
            {
                string lineID = base.Request.QueryString["SeqNO"];
                this.txtLineID.Text = lineID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["OwnerID"]))
            {
                string ownerID = base.Request.QueryString["OwnerID"];
                this.hdnOwnerID.Value = ownerID;
                this.txtOwnerID.Text = ownerID;
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.hdnType.Value = base.Request.QueryString["type"].ToString();
                this.SetDeliveryOrderDetail2From();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadDate();
            }
        }

        private void SetDeliveryOrderDetail2From()
        {
            if (!string.IsNullOrEmpty(this.hdnDeliveryOrderID.Value) && !string.IsNullOrEmpty(this.txtLineID.Text))
            {
                DeliveryOrderDetailInfo deliveryOrderDetailInfo = TransportDetailMgtServiceClientProxy.GetInstance().GetDeliveryOrderDetailInfoBypoIDAndLineID(this.hdnDeliveryOrderID.Value, this.txtLineID.Text.Trim());
                if (deliveryOrderDetailInfo != null)
                {
                    this.txtLineID.Text = deliveryOrderDetailInfo.LineID.ToString();
                    this.txtOwnerID.Text = deliveryOrderDetailInfo.OwnerID;
                    this.txtPackID.Text = deliveryOrderDetailInfo.PackID;
                    this.txtUomID.Text = deliveryOrderDetailInfo.UomID;
                    this.txtOriginalQty.Text = base.GetPreferenceDigit(deliveryOrderDetailInfo.OriginalQty.ToString());
                    this.txtLoadingQty.Text = base.GetPreferenceDigit(deliveryOrderDetailInfo.LoadingQty.ToString());
                    this.txtUdf1.Text = deliveryOrderDetailInfo.Udf1;
                    this.txtUdf2.Text = deliveryOrderDetailInfo.Udf2;
                    this.txtUdf3.Text = deliveryOrderDetailInfo.Udf3;
                    this.txtUdf4.Text = deliveryOrderDetailInfo.Udf4;
                    this.txtUdf5.Text = deliveryOrderDetailInfo.Udf5;
                    this.txtUdf6.Text = deliveryOrderDetailInfo.Udf6;
                    this.txtUdf7.Text = deliveryOrderDetailInfo.Udf7;
                    this.txtUdf8.Text = deliveryOrderDetailInfo.Udf8;
                    this.txtUdf9.Text = deliveryOrderDetailInfo.Udf9;
                    this.txtUdf10.Text = deliveryOrderDetailInfo.Udf10;
                    base.DataEntityViewState = deliveryOrderDetailInfo;
                }
            }
        }

        protected void txtSkuID_TextChanged(object sender, EventArgs e)
        {
            try
            {
                SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtOwnerID.Text.Trim(), this.txtSkuID.Text.Trim(), "", "SKU") as SkuInfo;
                if (!(((skuInfo == null) || string.IsNullOrEmpty(skuInfo.SkuID)) || string.IsNullOrEmpty(skuInfo.OwnerID)))
                {
                    this.txtPackID.Text = skuInfo.PackID;
                    this.txtSkuDescr.Text = skuInfo.Descr;
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F8100_010");
                    this.txtSkuID.Focus();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.txtSkuID.Focus();
            }
            this.hdnPageType.Value = "0";
        }
    }
}

